package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: thoughtworker
 * Date: 10/13/13
 * Time: 3:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class ReserveBooksMenu implements MenuOptions{
    private final PrintStream printStream;
    private final BufferedReader reader;
    private final Map<String, Integer> bookList;
    private Menu menu;


    private Collection<String> listBookOptions = Arrays.asList("Please enter a book name to reserve.");
    private String sorryMessage = "Sorry we don't have that book yet.";
    private CurrentUser currentUser;

    public ReserveBooksMenu(PrintStream printStream, BufferedReader reader, Map<String, Integer> bookList, CurrentUser currentUser) {
        this.printStream = printStream;
        this.reader = reader;
        this.bookList = bookList;
        this.currentUser = currentUser;

        this.menu = new Menu(this.printStream, this.reader, listBookOptions);

    }

    @Override
    public void execute() throws IOException {

        if (currentUser.getUser() == null) {
            printStream.println("You need to log in to reserve.");
        } else {
            printStream.println(bookList.toString());

            String bookName = menu.promptUserInput();
            if(bookList.containsKey(bookName)){
                if(bookList.get(bookName) > 0){
                    printStream.println("Thank You! Enjoy the book.");
                    bookList.put(bookName, bookList.get(bookName)-1);
                } else {
                    printStream.println(sorryMessage);
                }
            }else {
                printStream.println(sorryMessage);
            }
        }


    }
}
